package com.jxwifi.cloud.quickcleanserver.mycenter;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicActivity;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.CleanBasicActivity;
import com.jxwifi.cloud.quickcleanserver.app.UrlPool;
import com.jxwifi.cloud.quickcleanserver.bean.InfoBean;
import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;
import com.jxwifi.cloud.quickcleanserver.presenter.ContactNamePresenter;
import com.jxwifi.cloud.quickcleanserver.utils.PasswordLogin;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;
import com.jxwifi.cloud.quickcleanserver.view.ContactNameView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/5/21.
 */

public class ContactNameActivity extends CleanBasicActivity implements ContactNameView {

    @Bind(R.id.et_my_personal_data_name)
    EditText mEtMyPersonalDataName;
    private ContactNamePresenter mPresenter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_name);
        ButterKnife.bind(this);
        openEventBus();
        initView();
    }

    public void initView(){
        mEtMyPersonalDataName.setText(AccountMgr.getcontactName(mBaseContext));
        mPresenter=new ContactNamePresenter(this);
    }

    @OnClick({R.id.rel_img_back_pressed,R.id.tv_working_confirm})
    void clickCK(View v){
        switch (v.getId()){
            case R.id.rel_img_back_pressed:
                onBackPressed();
                break;
            case R.id.tv_working_confirm:
                mPresenter.initdata();
                break;
        }
    }

    @Override
    public String getContactName() {
        return mEtMyPersonalDataName.getText().toString();
    }

    @Override
    public void onSuccess(int code, String data) {
        if (code==0){
            InfoBean mInfoBean= JSON.parseObject(data, InfoBean.class);
            AccountMgr.putUser(mBaseContext,mInfoBean,false);
            finish();
        }else{
            PasswordLogin.passwordlogin(mBaseContext,TAG,AccountMgr.gettell(mBaseContext),AccountMgr.getpassword(mBaseContext));
            if (PasswordLogin.mIsLogin){
                mPresenter.initdata();
            }
        }
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast =Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast,3000);
    }

}
